package record;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.cardview.widget.CardView;

import com.qingning.qn.R;
import com.lzy.okgo.model.Progress;

import java.io.File;
import java.text.DecimalFormat;
import base.BaseActivity;
import base.httputils.HttpClient;
import base.httputils.OnDownloadCallBack;
import utils.StatusBarUtil;
import utils.ToastUtils;
import view.TitleWidget;

public class DownLoadFileActivity extends BaseActivity implements View.OnClickListener {
    private TitleWidget titleView;
    private TextView tvFileName, tvProgress, tvOver;
    private CardView cvDownload, cvOpen;
    private ProgressBar proBar;

    // 携带过来的本地文件夹，附件下载地址，要展示的文件名,本地文件命名
    private String filePath, downUrl, fileName,custName;

    private File file;//下载的附件

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_down_load_file);
        StatusBarUtil.setTranslucentForImageView(this,0,null);
        initView();
    }


    public void initView() {
        titleView = (TitleWidget) findViewById(R.id.title12);
        tvFileName = (TextView) findViewById(R.id.tvFileName);
        tvProgress = (TextView) findViewById(R.id.tvProgress);
        tvOver = (TextView) findViewById(R.id.tvOver);
        cvDownload = (CardView) findViewById(R.id.cvDownload);
        cvOpen = (CardView) findViewById(R.id.cvOpen);
        proBar = (ProgressBar) findViewById(R.id.proBar);

        downUrl = getIntent().getStringExtra("downUrl");
        filePath = Environment.getExternalStorageDirectory()+"/ahj/";
        fileName = getIntent().getStringExtra("fileName");
        custName = getIntent().getStringExtra("custName");

        tvFileName.setText(fileName);
        addListener();
    }


    public void addListener() {
        cvDownload.setOnClickListener(this);
        cvOpen.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == cvDownload.getId()) {
            // 开始下载
            final File f = new File(filePath);
            if (!f.exists()) {
                f.mkdirs();
            }
            HttpClient.downloadFile(this, downUrl, filePath, custName, new OnDownloadCallBack() {
                @Override
                public void success(File file) {
                    DownLoadFileActivity.this.file = file;
                    tvProgress.setVisibility(View.GONE);
                    proBar.setVisibility(View.GONE);
                    cvDownload.setVisibility(View.GONE);
                    tvOver.setVisibility(View.VISIBLE);
                    cvOpen.setVisibility(View.VISIBLE);
                    setResult(RESULT_OK);
                }

                @Override
                public void progress(Progress progress) {
                    if (cvDownload.getVisibility() == View.VISIBLE) {
                        tvOver.setVisibility(View.GONE);
                        cvOpen.setVisibility(View.GONE);

                        cvDownload.setVisibility(View.GONE);
                        tvProgress.setVisibility(View.VISIBLE);
                        proBar.setVisibility(View.VISIBLE);
                    }
                    tvProgress.setText("正在下载...(" + byte2MB(progress.currentSize) + "/" + byte2MB(progress.totalSize) + ")");
                    float fraction = progress.fraction * 100;
                    int pro = (int) fraction;
                    proBar.setProgress(pro);
                }

                @Override
                public void failed(String errorMsg) {
                    f.delete();
                    ToastUtils.showToast(DownLoadFileActivity.this,"下载失败，请稍后重试");
                    cvDownload.setVisibility(View.VISIBLE);
                    tvProgress.setVisibility(View.GONE);
                    proBar.setVisibility(View.GONE);
                    tvOver.setVisibility(View.GONE);
                    cvOpen.setVisibility(View.GONE);
                }
            });
        }

        if (view.getId() == cvOpen.getId()) {
            Log.e("eeee",file.getAbsolutePath());
            FileUtils.openF(this, file);
        }
    }

    private String byte2MB(long size) {
        StringBuffer bytes = new StringBuffer();
        DecimalFormat format = new DecimalFormat("###.0");
        double i = (size / (1024.0 * 1024.0));
        bytes.append(format.format(i)).append("MB");
        return bytes.toString();
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
